/*//GEN-LINE:initComponents
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorElementEdit.java
 *
 * Created on 06/10/2010, 13:53:11
 */

package vnetlab.ui.editor;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import vnetlab.elements.Element;
import vnetlab.elements.Interface;

/**
 *
 * @author rogeriosv
 */
public class EditorElementEdit extends javax.swing.JPanel implements TableModelListener{


    private boolean canEdit[][];
    private Element element;
    
    /** Creates new form EditorElementEdit */
    public EditorElementEdit() {
        initComponents();
        setElement(null);
        canEdit = new boolean[30][2];

        for(int i=0; i < canEdit.length;i++)
            for(int j=0; j < canEdit[i].length; j++)
                canEdit[i][j] = false;
    }

    public void setElement(Element element){        
        if(element != null){

            this.element=element;
            
            DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();

            dtm.setRowCount(0);

//            dtm.addRow(new Object[]{"Label", element.getLabel()});
//            canEdit[0][1] = true;
            
            dtm.addRow(new Object[]{"Name", element.getName()});
            
            dtm.addRow(new Object[]{"Type", element.getType()});
            
            dtm.addRow(new Object[]{"Id", element.getIdent()});
            
            dtm.addRow(new Object[]{"Created",String.valueOf(element.isCreated())});
            
            dtm.addRow(new Object[]{"Status", element.getStatus()});

            dtm.addRow(new Object[]{"Template", element.getTemplate()});
            
            dtm.addRow(new Object[]{"Interfaces #", element.numberOfNIC()});
            


            //Insere as interfaces
            String destine = null;
            for(int i= 0; i < element.getInterfaces().length; i++){
                Interface inter = element.getInterface(i);
                destine = inter.getDestine()== null ? "" :inter.getDestine().replace(element.getName(), "");

                dtm.addRow(new Object[]{"Interface ["+i+"]", destine});
                
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Element  Editor"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "", ""
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setModel(
            new javax.swing.table.DefaultTableModel(
                new Object [][] {},
                new String [] {"", ""}
            ){
                @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [rowIndex][columnIndex];
                }
            }
        );

        jTable1.getModel().addTableModelListener(this);
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jScrollPane1.setColumnHeader(null);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration                   

    public void tableChanged(TableModelEvent e) {
//        if( e.getType() == e.INSERT )
//             System.out.println("Inserindo...");
//        if( e.getType() == e.DELETE )
//             System.out.println("Apagando...");
        if( e.getType() == TableModelEvent.UPDATE ){
             if(this.element != null){
                 DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
                 int row = e.getFirstRow();
                 int column = e.getColumn();

                 if(column == 1){
                        switch(row){
                            case 0: element.setLabel((String)dtm.getValueAt(row, column)); break;
                            case 1: break;
                            case 3: break;
                            case 4: break;
                            case 5: break;
                            case 6: break;
                            case 7: break;
                        }
                 }
            }
        }
    }

}
